package com.xrh.core.generater.mongo.generater;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xrh.core.generater.bean.UserInput;
import com.xrh.core.generater.interfaces.Console;
import com.xrh.core.generater.utils.FileUtils;
import com.xrh.core.generater.utils.JavaFileFilter;
import com.xrh.core.generater.utils.OSUtils;
import com.xrh.core.generater.utils.PathUtils;



public class MongoGeneraterUI  extends JFrame implements  Console  {
	
	//缓存文件名称
	public final static String CACHE_FILE_NAME = "mongoUserInput.cache";
	
	//缓存的路径
	public final static String CACHE_FILE_PATH = PathUtils.getUserHomePath()  + File.separator + "JavaApp"+File.separator+"CodeGenerater" + File.separator + CACHE_FILE_NAME ;
	
	private static final long serialVersionUID = 8500817386469482382L;
	private JTextField controllerPackageInput;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField servicePackageInput;
	private JTextField dtoPackageInput;
	private JTextField addRequestInput;
	private JTextField queryRequestPackageInput;
	private JTextField updateRequestPackageInput;
	private JTextField chooseJavaFilesInput;
	private JTextArea textArea;
	private JButton btnNewButton;
	private JTextField outDirPathInput;
	private JTextField entityPackageInput;
	private JTextField authorInput;
	
	public MongoGeneraterUI() {
		
		//关闭窗口
		this.addWindowListener(new WindowAdapter() {
	            public void windowClosing(WindowEvent e) {
	                System.exit(0);
	        	};
		});
		
		MongoGeneraterUI mybatisPlusGeneraterUI = this;
		
		setBounds(100, 100, 674, 524);
		setResizable(false);
		setTitle("Mongo代码生成器");
		
		getContentPane().setLayout(new CardLayout(0, 0));
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, "name_90880623278465");
		
		JLabel lblNewLabel_2_3 = new JLabel("controller包路径 :");
		
		controllerPackageInput = new JTextField();
		controllerPackageInput.setText("com.sy.km.service.controllers");
		controllerPackageInput.setColumns(10);
		
		JLabel lblNewLabel_2_3_2 = new JLabel("updateRequest包路径 :");
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		
		JLabel lblNewLabel_2_3_1_1 = new JLabel("updateRequest包路径 :");
		
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		
		JLabel lblNewLabel_2_3_1 = new JLabel("             service包路径 :");
		
		servicePackageInput = new JTextField();
		servicePackageInput.setText("com.sy.km.service.service");
		servicePackageInput.setColumns(10);
		
		JLabel lblNewLabel_2_3_3 = new JLabel("                      dto包路径 :");
		
		dtoPackageInput = new JTextField();
		dtoPackageInput.setText("com.sy.km.api.dto");
		dtoPackageInput.setColumns(10);
		
		JLabel addRequestPackage = new JLabel("   addRequest包路径 :");
		
		addRequestInput = new JTextField();
		addRequestInput.setText("com.sy.km.api.request.addRequest");
		addRequestInput.setColumns(10);
		
		JLabel lblNewLabel_2_3_4 = new JLabel("updateRequest包路径 :");
		
		JLabel lblNewLabel_2_3_1_3 = new JLabel("queryRequest包路径 :");
		
		queryRequestPackageInput = new JTextField();
		queryRequestPackageInput.setText("com.sy.km.api.request.queryRequest");
		queryRequestPackageInput.setColumns(10);
		
		updateRequestPackageInput = new JTextField();
		updateRequestPackageInput.setText("com.sy.km.service.request.updateRequest");
		updateRequestPackageInput.setColumns(10);
		
		JSeparator separator = new JSeparator();
		
		JSeparator separator_1 = new JSeparator();
		
		JLabel lblNewLabel_2_3_3_1_1 = new JLabel("                  控制台输出 :");
		
		btnNewButton = new JButton("生成代码");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnNewButton.setEnabled(false);
				//点击代码生成按钮 , 触发生成代码
				new  MongoGenerater().generateFiles(mybatisPlusGeneraterUI, getUserInput());
			}
		});
		
		JLabel lblNewLabel_2_3_4_1_1 = new JLabel("               选择entity文件 :");
		
		chooseJavaFilesInput = new JTextField();
		chooseJavaFilesInput.setForeground(Color.BLUE);
		chooseJavaFilesInput.setBackground(Color.LIGHT_GRAY);
		chooseJavaFilesInput.setEditable(false);
		chooseJavaFilesInput.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("打开");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				JFileChooser jfc = new JFileChooser();
				//只能选择java文件
//				 jfc.addChoosableFileFilter(new JavaFileFilter("java"));  //这个可以过滤多个
				//可以选择多个文件
				jfc.setMultiSelectionEnabled(true);
				jfc.setFileFilter(new JavaFileFilter("java"));
				jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); //去掉所有文件的选项
		        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );   //只能打开文件
		        jfc.setDialogTitle("选择输出目录");
		        jfc.showDialog(new JLabel(), "确定");
		        
		        File[] files =  jfc.getSelectedFiles();
		        
		        StringBuilder sb = new StringBuilder();
		        
		        for(int i = 0 ; i < files.length ; i ++) {
		        	
		        	File file = files[i];
		        	
		        	if(!file.exists()) continue;
		        	
		        	sb.append(",").append(file.getAbsolutePath());
		        }
		        
		        if(sb.length() > 0) {
		        	chooseJavaFilesInput.setText(sb.substring(1));
		        }
		        
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		JButton btnNewButton_2 = new JButton("退出");
		btnNewButton_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				 System.exit(0);
			}
		});
		
		JButton btnNewButton_3 = new JButton("生成代码");
		btnNewButton_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnNewButton.setEnabled(false);
				//点击代码生成按钮 , 触发生成代码
				new  MongoGenerater().generateFiles(mybatisPlusGeneraterUI, getUserInput());
			}
		});
		
		JButton btnNewButton_4 = new JButton("打开输出目录");
		btnNewButton_4.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				UserInput userInput = getUserInput();
				
				String[] cmd = new String[]{"cmd.exe","/c" , "start " + userInput.getOutputDirPath()};
				
				//不是windows这样打开
				if(!OSUtils.isWindows()) 
					cmd = new String[]{"bash","-c" , "open " + userInput.getOutputDirPath()};
				
				try {
					Runtime.getRuntime().exec(cmd);
				} catch (IOException e1) {
					console(e1.getMessage());
				}
				
			}
		});
		
		JLabel outDirPathLabel = new JLabel("                选择输出目录 :");
		
		outDirPathInput = new JTextField();
		outDirPathInput.setText(PathUtils.getDesktopPath() + File.separator + "Mongo代码生成器" );
		outDirPathInput.setForeground(Color.BLUE);
		outDirPathInput.setEditable(false);
		outDirPathInput.setColumns(10);
		outDirPathInput.setBackground(Color.LIGHT_GRAY);
		
		JButton btnNewButton_1_1 = new JButton("打开");
		btnNewButton_1_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser jfc = new JFileChooser(); 
		        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );  //只能打开目录
		        jfc.setDialogTitle("选择输出目录");
		        jfc.showDialog(new JLabel(), "确定");  
		        File file = jfc.getSelectedFile();  
		        if(file != null && file.exists()){
		        	outDirPathInput.setText(file.getAbsolutePath() + File.separator + "Mongo代码生成器");
		        }
			}
		});
		
		JLabel entityPackageInputLabel = new JLabel("                   entity包路径 :");
		
		entityPackageInput = new JTextField();
		entityPackageInput.setText("com.sy.km.api.entity");
		entityPackageInput.setColumns(10);
		
		JLabel authorInputLabel = new JLabel("作者:");
		
		authorInput = new JTextField();
		authorInput.setText("SY-CodeGenerater");
		authorInput.setColumns(10);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
								.addComponent(btnNewButton)
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lblNewLabel_2_3_2, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
											.addGap(6))
										.addGroup(gl_panel.createSequentialGroup()
											.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_panel.createSequentialGroup()
													.addComponent(outDirPathLabel, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
													.addGap(6)
													.addComponent(outDirPathInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
													.addGap(6)
													.addComponent(btnNewButton_1_1, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
												.addGroup(gl_panel.createSequentialGroup()
													.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(ComponentPlacement.RELATED)
													.addComponent(lblNewLabel_2_3_3_1_1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(ComponentPlacement.RELATED)
													.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 454, GroupLayout.PREFERRED_SIZE)))
											.addGap(13)
											.addComponent(separator, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
											.addGap(58))
										.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
											.addComponent(entityPackageInputLabel, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
											.addGap(6)
											.addComponent(entityPackageInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
											.addGap(98)
											.addComponent(authorInputLabel, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(authorInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)))
									.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(12)
									.addComponent(lblNewLabel_2_3_1_1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnNewButton_2))
						.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
							.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblNewLabel_2_3)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(controllerPackageInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(lblNewLabel_2_3_1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(servicePackageInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_2_3_4, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(updateRequestPackageInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(lblNewLabel_2_3_1_3, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(queryRequestPackageInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_2_3_3, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
									.addComponent(dtoPackageInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(addRequestPackage, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addGap(6)
									.addComponent(addRequestInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_2_3_4_1_1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(chooseJavaFilesInput, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnNewButton_1))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(btnNewButton_3)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(btnNewButton_4)))))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(20)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2_3_4_1_1)
						.addComponent(chooseJavaFilesInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(controllerPackageInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2_3_1)
						.addComponent(servicePackageInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2_3))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblNewLabel_2_3_3))
						.addComponent(dtoPackageInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(addRequestPackage))
						.addComponent(addRequestInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblNewLabel_2_3_4))
						.addComponent(updateRequestPackageInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblNewLabel_2_3_1_3))
						.addComponent(queryRequestPackageInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(6)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(entityPackageInputLabel))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(entityPackageInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(authorInputLabel)
							.addComponent(authorInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(outDirPathLabel))
						.addComponent(outDirPathInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton_1_1))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(separator, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(lblNewLabel_2_3_3_1_1)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(15)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 172, GroupLayout.PREFERRED_SIZE)))
					.addGap(6)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(186)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnNewButton)
								.addComponent(btnNewButton_2)))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnNewButton_4)
							.addComponent(btnNewButton_3)))
					.addGap(252)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(79)
							.addComponent(lblNewLabel_2_3_2))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(73)
							.addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(79)
							.addComponent(lblNewLabel_2_3_1_1))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(73)
							.addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
		);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		textArea.setForeground(Color.BLUE);
		scrollPane.setViewportView(textArea);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textArea.setBackground(Color.LIGHT_GRAY);
		textArea.setEditable(false);
		panel.setLayout(gl_panel);
		
		
		//从缓存读取配置并初始化
		readCacheAndInitForm();
	}


	public static void main(String[] args) {
		
		try {
			UIManager.setLookAndFeel(new NimbusLookAndFeel());
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MongoGeneraterUI frame = new MongoGeneraterUI();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * @function 读取缓存文件并初始化表单
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void  readCacheAndInitForm() {
		
		File file = new File(CACHE_FILE_PATH);
		
		console("欢迎你使用代码生成器~");
		
		if(!file.exists()) 
			return;
		
		console("从缓存文件" +  CACHE_FILE_NAME + "读取上一次的配置信息...");
		
		try {
			String txt = FileUtils.readFileToString(CACHE_FILE_PATH);
			UserInput userInput = JSONObject.parseObject(txt, UserInput.class);
			setFormByUserInput(userInput);
		} catch (IOException e) {
			console("读取失败..");
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @function 缓存用户输出
	 * @author 肖荣辉
	 * @throws IOException 
	 * @date 2021年7月21日
	*/
	public void cacheUserInput(UserInput userInput)  {
		
		try {
			
			String txt = JSON.toJSONString(userInput);
			
			FileUtils.saveTxtFile(CACHE_FILE_PATH, txt);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @function 根据userInput设置表单
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void setFormByUserInput(UserInput userInput) {
		
		controllerPackageInput.setText(userInput.getControllerPackage());
		
		servicePackageInput.setText(userInput.getServicePackage());
		
		dtoPackageInput.setText(userInput.getDtoPackage());
		
		addRequestInput.setText(userInput.getAddRequestPackage());
		
		updateRequestPackageInput.setText(userInput.getUpdateRequestPackage());
		
		queryRequestPackageInput.setText(userInput.getQueryRequestPackage());
		
		entityPackageInput.setText(userInput.getEntityPackage());
		
		outDirPathInput.setText(userInput.getOutputDirPath());
		
		authorInput.setText(userInput.getAuthorName());
	}
	
	
	/**
	 * @function 获取用户输入
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public  UserInput getUserInput() {
		
		String controllerPackage = StringUtils.trim(controllerPackageInput.getText());
		String servicePackage = StringUtils.trim(servicePackageInput.getText());
		String dtoPackage = StringUtils.trim(dtoPackageInput.getText());
		String addRequestPackage = StringUtils.trim(addRequestInput.getText());
		String updateRequestPackage = StringUtils.trim(updateRequestPackageInput.getText());
		String queryRequestPackage = StringUtils.trim(queryRequestPackageInput.getText());
		String outputDirPath =  StringUtils.trim(outDirPathInput.getText());
		String entityPackage =  StringUtils.trim(entityPackageInput.getText());
		String author =  StringUtils.trim(authorInput.getText());
		
		UserInput userInput	= new UserInput();
		userInput.setControllerPackage(controllerPackage);
		userInput.setServicePackage(servicePackage);
		userInput.setDtoPackage(dtoPackage);
		userInput.setAddRequestPackage(addRequestPackage);
		userInput.setUpdateRequestPackage(updateRequestPackage);
		userInput.setQueryRequestPackage(queryRequestPackage);
		userInput.setEntityPackage(entityPackage);
		
		userInput.setAuthorName(StringUtils.isBlank(author) ? "SY-CodeGenerater" : author);
		userInput.setChooseJavaFiles(chooseJavaFilesInput.getText());
		userInput.setOutputDirPath(outputDirPath);
		
		return userInput;
	}
	
	/**
	 * @function 控制台输出信息
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void console(String msg) {
		textArea.setText(textArea.getText() + "\n" + msg);
	}
	
	/**
	 * @function 启用生成按钮
	 * @author 肖荣辉
	 * @date 2021年7月21日
	*/
	public void enableGeneraterBtn() {
		btnNewButton.setEnabled(true);
	}
}
